package org.pombo.sqm;

import org.pombo.gtm.*;

class ChanceNodeProbabilityCalculator implements NodeProbabilityCalculator<ChanceNode> {

	private ChanceNode node;
	private ProbabilityCalculatorFactory calculate;

	ChanceNodeProbabilityCalculator(ProbabilityCalculatorFactory probabilityCalculatorFactory) {
		this.calculate = probabilityCalculatorFactory;
	}

	public void setNode(ChanceNode node) {
		this.node = node;
	}

	public NodeProbabilities nodeProbabilities(double baseProbability) {
		NodeProbabilities nodeProbabilities = new NodeProbabilities();
		for (Action action : this.node.getActions()) {
			double branchProbability = this.node.getProbabilityForAction(action) * baseProbability;
			NodeProbabilities branchProbabilities = this.calculate.on(this.node.getNodeForAction(action)).nodeProbabilities(branchProbability);
			nodeProbabilities.add(branchProbabilities);
		}
		return nodeProbabilities;
	}

}
